<?php
 //近标准格式打印输出数组
	function p($array){
        dump($array,1,'<pre>',0);
    }
    
    //返回指定UserId的User实体
    function getUser($uId){
        return M('user')->where("UserId='$uId'")->select();
    }
    
    //返回指定UserId的购物车细目
    function getCartItems($uId){
        return M('Cart_item')->where("UserId='$uId'")->select();
    }
    
    
    //返回指定ProId的商品
    function getProducts($pId){
        return M('products')->where("ProId='$pId'")->select();
    }
    
    //处理CartItem并返回应用于模板的数据
    function handleCartItem($cartItem){
        $array= array();
        for ($i=0; $i < count($cartItem); $i++) {
            //array_push($array[$i],getProducts($cartItem[$i]['ProId'])[0]);
           array_push($array,
               array_merge(
                   $cartItem[$i],
                   getProducts($cartItem[$i]['ProId'])[0]
                   )
            );
        }
        return $array;
    }
    
    //返回指定UserId的订单
    function getOrders($uId){
        return M('order')->where("UserId='$uId'")->select();
    }
    
    //返回指定订单的所有订单细目
    function getOrderItem($oId){
        return M('order_item')->where("OrderId='$oId'")->select();
    }
    
    //处理订单细目并返回应用于模板的数据
    function handleOrderItem($orderItem){
        //订单细目
        $item= array();
        for ($i=0; $i < count($orderItem); $i++) {
        array_push($item,
            array_merge(
                $orderItem[$i],
                getProducts($orderItem[$i]['ProId'])[0]
                )
            );
        }
        return $item;
    }
    //处理订单并返回应用于模板的数据
    function handleOrder($order){
        
        for ($i=0; $i < count($order); $i++) {
            array_push($order[$i],handleOrderItem(getOrderItem($order[$i]['OrderId'])));       
        }
        return $order;
    }
    
    //按状态取订单
    function splitOrder($data,$state){
        $array=array();
        for ($i=0; $i < Count($data); $i++) { 
            if($data[$i]['OrderState']==$state)
                array_push($array,$data[$i]);
        }
        return $array;
    }
    
    //删除用户地址，并返回结果
    function removeAddress($aId){
        $result=M('user_info')->where("InfoId='$aId'")->delete();
        return $result;
    }
    
    //根据用户名返回指定的用户地址信息集
    function getAddressSet($uId){
        return M('user_info')->where("UserId='$uId'")->select();
    }
    
    function imgUpLoad(){
        $info;
        import('ORG.Net.UploadFile');
        $upload=new UploadFile();
        $upload->maxSize=3145728;
        $upload->allowExts=array('jpg','gif','png','jpeg');
        $upload->savePath='./Public/images/Products/';
        if(!$upload->upload()){
            $this->error($upload->getErrorMsg());
        }else{
            $info=$upload->getUploadFileInfo();
        }
        return $info;
    }
    function RemoveImg($id){
        $data=M("Products")->where(array('ProID'=>$id))->select();
        $img=$data[0]['ProImg'];
        $imgUrl="./Public/images/Products/".$img;
        unlink($imgUrl);
    }
    
    //计算该用户购物车选中物品总价
    function reckon($uId){
        $temp= M('Cart_item')->where("UserId='$uId' and YnCheck='checked'")->select();
        $total=0.00;
        foreach ($temp as $key => $value) {
            $total+=($temp[$key]['ProCount']*
                getProducts($temp[$key]['ProId'])[0]['ProPrice']);
        }
        return $total;
    }
    
    //清理指定用户的购物车
    function clearCart($uId){
        $temp= M('Cart_item')->where("UserId='$uId' and YnCheck='checked'")->delete();
    }
    
    //设置、修改订单的收货地址
    function modifyOrder($oId,$ufId){
        $uf=M('user_info')->where("InfoId='$ufId'")->select()[0];
        $data['UserName']=$uf['TakeName'];
        $data['Address']=$uf['TakeAddress'];
        $data['UserPhone']=$uf['TakePhone'];
        $result=M('order')->where("OrderId='$oId'")->save($data);
        return $result;
    }
    
    //查找商品 返回商品Id
    function seachPro($pName){
        $resultList=array();
        //echo mb_substr($pName,0,2);
        for ($i=0; $i <mb_strlen($pName) ; $i++) { 
            //p(mb_substr($pName,0,$i));
            if(mb_substr($pName,$i,1)!=""){
                $resultList=array_merge($resultList,seach(mb_substr($pName,$i,1)));
                
            }
        }
        return array_unique($resultList);
    }
    
    function seach($str){
        
        $data=M('products')->select();
        $rex='/.*'.$str.'.*/';//正则表达式
        $result=array();//存放结果
        
        //echo $str.'<br/>';
        //echo $rex.'<br/>';
        
        foreach ($data as $key => $value) {
            $pName=$value['ProName'];
            //echo preg_match($rex,$pName).'<br/>';
            if(preg_match($rex,$pName))
                array_push($result,$value['ProID']);
        }
        
        return $result;
    }
    
    //清理默认值
    function initValue($uId){
        $data=M('user_info')->where("UserId='$uId'")->select();
        for ($i=0; $i <count($data) ; $i++) { 
            $temp['InfoId']=$data[$i]['InfoId'];
            $temp['YnDefault']='';
            //p($temp);
            M('user_info')->save($temp);
        }
        //p(M('user_info')->where("Userid='$uId'")->select());
    }
    
    //商品售后信息更新  pCount售出量
    function updateProInfo($pId,$pCount,$state){
        //echo 'Id:'.$pId.'<br/>';
        //echo 'Count:'.$pCount.'<br/>';
        $temp=getProducts($pId);
        
        $data['ProID']=$pId;
        if($state==0){//立即付款 商品库存-1 商品销量+1
            $data['ProSaleCount']=intval($temp[0]['ProSaleCount'])+$pCount;
            $data['ProNum']=intval($temp[0]['ProNum'])-$pCount;
        }else if($state==1){//提交订单  商品库存-1
            $data['ProNum']=intval($temp[0]['ProNum'])-$pCount;
        }else if($state==2){//付款    商品销量+1
            $data['ProSaleCount']=intval($temp[0]['ProSaleCount'])+$pCount;
        }
            
        
        //p($data);
        return M('Products')->save($data); 
    }
    
    //返回订单商品数
    function getOrderProCount($oId){
        $count=0;
        $temp=getOrderItem($oId);
       // p( $temp[0]['ProCount']);
        foreach ($temp as $key => $value) {
            $count=$count+intval($value['ProCount']);
            //p('/'.$value['ProCount'].'/');
        }
        
        return $count;
    }
    
    //取商品库存
    function getProCount($pId){
        return getProducts($pId)[0]['ProNum'];
    }
    
    //检测订单中商品库存是否满足销售
    function checkOrder($oId){
        $oId=getOrderItem($oId);
        if(count($oId)<=0)
            return false;
        foreach ($oId as $key => $value) {
            p('pId'.getProCount($value['ProId']));
             p('pCount'.$value['ProCount']);
            if(getProCount($value['ProId'])<$value['ProCount'])
                return false;
        }
        return true;
    }
    
    //检测是否满足提交订单
    function checkSubOrder($uId){
        $uCart=getCartItems($uId);
        if(count($uCart)<=0)
            return false;
       
        foreach ($uCart as $key => $value) {
           if($value['ProCount']>getProCount($value['ProId']))
                return false;
        }
        return true;
        
    }
    
    //检测订单中的商品是否被评价
    function checkOrderV($oId,$pId){
        $temp=M('order_item')->where("OrderId='$oId' and ProId='$pId'")->select();
        if(count($temp)<=0)
            return -1;
        return $temp[0]['ynValuation'];
    }
    
?>